#include "cmdinswartimetest.h"
#include "cppunit/BriefTestProgressListener.h"
#include "cppunit/CompilerOutputter.h"
#include "cppunit/extensions/TestFactoryRegistry.h"
#include "cppunit/TestResult.h"
#include "cppunit/TestResultCollector.h"
#include "cppunit/TestRunner.h"
#include <stdlib.h>
#include <stdio.h>

#include "mainctrl.h"
CMainCtrl g_stctrl;

int Test_InitBuildingRank()
{
	FILE *fpFile = fopen("buildingrank.txt","r");
	if (fpFile == NULL)
	{
		printf("Can't open buildingrank.txt\n");
		return -1;
	}

	char szReadLine[1024] = {0};
	while(!feof(fpFile))
	{
		memset(szReadLine,0,sizeof(szReadLine));
		fgets(szReadLine,sizeof(szReadLine),fpFile);
		if (strlen(szReadLine) <=0)
		{
			continue;
		}
		CBuildingRank stBuildingRank;
		int iIndex = 0;
		if (szReadLine[0] == '#')
		{
			printf("comment line\n");
			continue;
		}
		int iBuildRank;
		char szPrecondition[64] = {0};
		char szSpecial[64] = {0};

		int col = sscanf(szReadLine,"%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%s%s",
			&stBuildingRank.iBuildingID,&iBuildRank,&stBuildingRank.iData,&stBuildingRank.iNeedWood,&stBuildingRank.iNeedIron,&stBuildingRank.iNeedFood,
			&stBuildingRank.iNeedStone,&stBuildingRank.iNeedPopulation,&stBuildingRank.iNeedTime,&stBuildingRank.iNeedMoney,
			&stBuildingRank.iReserve1,&stBuildingRank.iReserve2,&stBuildingRank.iReserve3,&stBuildingRank.iReserve4,&stBuildingRank.iReserve5,
			&stBuildingRank.iReserve6,&stBuildingRank.iReserve7,&stBuildingRank.iReserve8,
			szPrecondition,
			szSpecial);
		if (col < 20)
		{
			printf("bad line:%s\n",szReadLine);
			continue;
		}
		stBuildingRank.chRank = (char)iBuildRank;

		if (szPrecondition[0]=='_')
		{
			szPrecondition[0] = 0;
		}
		if (szSpecial[0] == '_')
		{
			szSpecial[0] = 0;
		}

		stBuildingRank.szPrecondition.Set(szPrecondition);
		stBuildingRank.szNeedSpecial.Set(szSpecial);

		iIndex = g_stctrl.m_stBuildingRankHash.HashAlloc(CT_UINT_MAKE(stBuildingRank.iBuildingID, stBuildingRank.chRank),false);
		g_stctrl.m_stBuildingRankHash[iIndex] = stBuildingRank;
	}
	fclose(fpFile);

	return 0;
}

int Test_Initialize()
{
	g_stctrl.Test_Init();
	Test_InitBuildingRank();
	return 0;
}


int main(int argc, char * argv[])
{

	Test_Initialize();


	//Create the event manager and test controller
	CPPUNIT_NS::TestResult controller;

	//Add a listener that collects test result
	CPPUNIT_NS::TestResultCollector result;
	controller.addListener(&result);

	//Add a listener that print dots as test run
	CPPUNIT_NS::BriefTestProgressListener progress;
	controller.addListener(&progress);

	//Add the top suite to the test runner 
	CPPUNIT_NS::TestRunner runner;
	runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());

	runner.run(controller);

	//Print test in a compiler compatibale format
	CPPUNIT_NS::CompilerOutputter outputter(&result, std::cerr);

	outputter.write();

	return result.wasSuccessful() ? 0 : 1;

}
